package fr.generali.accueilclient.gwt.client.ui.components.form.formitem;

import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.user.client.ui.CheckBox;


public class CheckBoxFormItem extends FormItem<Boolean> implements LeafValueEditor<Boolean> {

    private CheckBox editCheckBox;

    private CheckBox readCheckBox;

    public CheckBoxFormItem() {
        super(new CheckBox(), new CheckBox());
        editCheckBox = ((CheckBox ) editableWidget);
        editCheckBox.setEnabled(true);
        readCheckBox = ((CheckBox ) readOnlyWidget);
        readCheckBox.setEnabled(false);
        nameLabel.setText("");

        editCheckBox.addStyleName(res.style().checkBoxText());
        readCheckBox.addStyleName(res.style().checkBoxText());

        separator.removeStyleName(res.style().separatorContent());

        editCheckBox.removeStyleName(res.style().input());
        readCheckBox.removeStyleName(res.style().input());
    }

    @Override
    protected boolean isDataFilled() {
        return true;
    }

    @Override
    protected void updateStyleFromMandatoryAndEditingStatus(boolean mandatory, boolean editing) {
        super.updateStyleFromMandatoryAndEditingStatus(mandatory, editing);
        separator.removeStyleName(res.style().separatorContent());
    }

    @Override
    protected void doSetValue(Boolean value, boolean valueIsNullOrEmpty) {
        editCheckBox.setValue(value);
        readCheckBox.setValue(value);
    }

    @Override
    public void setLabel(String label) {
        editCheckBox.setText(label);
        readCheckBox.setText(label);
    }

    @Override
    protected boolean valueIsNullOrEmpty(Boolean value) {
        return value == null;
    }

    @Override
    public Boolean getValue() {
        return editCheckBox.getValue();
    }

}
